/***************************************************************************************
 *  Name:    user_task.c
 *  Author:  cmd
 *  Created: 2025-01-25
 *  version: 1.0.0
 *  Description: This is a user task c file, in order to collect all app func except os.
 *
 *  Modification History:
 *  
 **************************************************************************************/
#include "delay.h"
#include "led.h"
#include "lcd.h"
#include "sys.h"
#include "aht10.h"
#include "communicate.h"
#include <stdio.h>


typedef struct
{
    u32 temp_int;
    u32 temp_frac;
    u32 humi_int;
    u32 humi_frac;
}task_thmt_data_t;

#define TASK_GET_FRACT(_x, _y)          ((u32)(((_x) - (float)(_y)) * 100))
#define TASK_THMT_BUF_SIZE              (sizeof(task_thmt_data_t))

void task_led0(void)
{
    u32 runTimes = 0;
    for (;;)
    {
        runTimes++;
        if (0 == runTimes % 3000000)
        {
//            LED_R(0);
//            LED_B(1);
//            LED_G(0);
            LOG_NON("task0 run t:%d \r\n", runTimes/1000);
			LOG_NON("\r\n");
        }
    }
}

void task_led1(void)
{
    u32 runTimes = 0;
    for (;;)
    {
        runTimes++;
        if (0 == runTimes % 3000000)
        {
            LED_R(0);
            LED_B(0);
            LED_G(1);
            LOG_NON("task1 run t:%d \r\n", runTimes/1000);
			LOG_NON("\r\n");
        }
    }
}

void task_tempHumi(void)
{
    fram_msg_t sMsg;
    char buf[TASK_THMT_BUF_SIZE] = {0};
    fram_msgData_t sData = {TASK_THMT_BUF_SIZE, &buf};
    sMsg.task_num = FRAM_TASK_THMT;
    sMsg.task_type = FRAM_TGT_EVENT;
    sMsg.msg = sData;
    u32 tick = 0;
    for (;;)
    {
        if (0 == tick % 1000)
        {
            float temperature = AHT10_Read_Temperature();
            float humi = AHT10_Read_Humidity();
            task_thmt_data_t tmp;
            tmp.temp_int = (u32)temperature;
            tmp.temp_frac = TASK_GET_FRACT(temperature, tmp.temp_int);
            tmp.humi_int = (u32)humi;
            tmp.humi_frac = TASK_GET_FRACT(humi, tmp.humi_int);
            memcpy(sMsg.msg.puData, &tmp, TASK_THMT_BUF_SIZE);
            xQueueSend(commHdl, &sMsg, 0);
        }
        ++tick;
    }
}

void task_lcdShow(void)
{

    while(1)
    {
        LCD_Clear(WHITE);
        POINT_COLOR = RED;
        BACK_COLOR = WHITE;
        LCD_ShowString(0, 100, 240, 32, 32, "DISPALY TEST:");

        POINT_COLOR = BLACK;
        BACK_COLOR = WHITE;
        task_thmt_data_t thmtData = {0};
        fram_msgData_t msgData;
        if (pdPASS = xQueueReceive(commThmt, &msgData, 0))
        {
            memcpy(&thmtData, msgData.puData, msgData.ucDataLenthl);
        }

        LCD_ShowNum(50, 170, thmtData.temp_int, 4, 24);
        LCD_ShowChar(100, 170, '.', 24);
        LCD_ShowNum(110, 170, thmtData.temp_frac, 2, 24);

        LCD_ShowNum(50, 200, thmtData.humi_int, 4, 24);
        LCD_ShowChar(100, 200, '.', 24);
        LCD_ShowNum(110, 200, thmtData.humi_frac, 2, 24);

        LCD_ShowString(0, 170, 240, 16, 24, "temp: ");
        LCD_ShowString(0, 200, 240, 12, 24, "humi: ");
        delay_ms(10000);
    }

}


